package com.phs.main.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phs.main.common.constant.AjaxResult;
import com.phs.main.web.po.Complaint;
import com.phs.main.web.po.PropertyInformation;
import com.phs.main.web.po.SysUsers;
import com.phs.main.web.service.IComplaintService;
import com.phs.main.web.service.IPropertyInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 投诉表 前端控制器
 * </p>
 *
 * @author Trent
 * @since 2020-07-07
 */
@Api
@RestController
@RequestMapping("/complaint")
public class ComplaintController {
	@Lazy
	@Autowired
	IComplaintService complaintService;
	@Lazy
	@Autowired
	IPropertyInformationService propertyInformationService;

	@ApiOperation(value = "添加投诉记录")
	@PostMapping("/addComplaint")
	public AjaxResult addRepair(@RequestBody Map<String, String> map) {
		try {
			QueryWrapper<PropertyInformation> propInfoWrapper = new QueryWrapper<>();
			propInfoWrapper.eq("house_unit", map.get("houseUnit"))
					.eq("house_name", map.get("houseName"));
			PropertyInformation one = propertyInformationService.getOne(propInfoWrapper);
			Date t = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			System.out.println(sdf.format(t));
			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

			LocalDateTime ldt = LocalDateTime.parse(sdf.format(t),df);
			System.out.println("String类型的时间转成LocalDateTime："+ldt);
			Complaint complaint = new Complaint();
			complaint.setUserName(map.get("userName"));
			complaint.setPhone(map.get("phone"));
			complaint.setCDetail(map.get("cDetail"));
			complaint.setUserId(((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId());
			complaint.setCTime(ldt);
			complaint.setHouseId(one.getHouseId());
			complaintService.save(complaint);
			return AjaxResult.success("添加成功");
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}

	@ApiOperation(value = "删除", notes = "根据ID删除投诉")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "投诉ID", dataType = "Long", required = true),
	})
	@DeleteMapping("deleteComplaintById/{id}")
	public AjaxResult deleteComplaintById(@PathVariable long id) {
		try {
			if (complaintService.removeById(id)) {
				return AjaxResult.success("已删除");
			} else {
				return AjaxResult.error("未删除");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("更新投诉")
	@PutMapping("/updateComplain")
	public AjaxResult updateComplain(@RequestBody Complaint complaint) {
		try {
			if (complaintService.updateById(complaint)) {
				return AjaxResult.success("更改成功");
			} else {
				return AjaxResult.error("更改失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("查询所有投诉")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "页码", dataType = "Long"),
			@ApiImplicitParam(name = "size", value = "单页条数", dataType = "Long")
	})
	@GetMapping("/findAll/{current}/{size}")
	public AjaxResult findAll(@PathVariable Long current, @PathVariable(required = false) Long size) {
		if (current == null) {
			current = 1L;
		}
		if (size == null) {
			size = 10L;
		}
		Page<Complaint> page = new Page<>(current , size);

		return AjaxResult.success(complaintService.page(page));
	}

	@ApiOperation("按照用户id和投诉状态查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cstatus", value = "投诉状态", dataType = "Long")
	})
	@GetMapping("/findByIDStatus/{cstatus}")
	public AjaxResult findByIDStatus(@PathVariable Long cstatus) {
		QueryWrapper<Complaint> complaintQueryWrapper = new QueryWrapper<>();
		complaintQueryWrapper.like("c_status", cstatus).like("user_id", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId());
		return AjaxResult.success(complaintService.list(complaintQueryWrapper));
	}

	@ApiOperation("按照投诉详情查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cDetail", value = "业主姓名", dataType = "String")
	})
	@GetMapping("/findByIDDetail/{cDetail}")
	public AjaxResult findByIDDetail(@PathVariable String cDetail) {
		QueryWrapper<Complaint> complaintQueryWrapper = new QueryWrapper<>();
		complaintQueryWrapper.like("user_id", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId()).like("c_detail", cDetail);
		return AjaxResult.success(complaintService.list(complaintQueryWrapper));
	}


	@ApiOperation("按照投诉状态查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cstatus", value = "投诉状态", dataType = "Long")
	})
	@GetMapping("/findByStatus/{cstatus}")
	public AjaxResult findByStatus(@PathVariable Long cstatus) {
		QueryWrapper<Complaint> complaintQueryWrapper = new QueryWrapper<>();
		complaintQueryWrapper.like("c_status", cstatus);
		return AjaxResult.success(complaintService.list(complaintQueryWrapper));
	}



	@ApiOperation("按照投诉人查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userName", value = "业主姓名", dataType = "String")
	})
	@GetMapping("/findByName/{userName}")
	public AjaxResult findByName(@PathVariable String userName) {
		QueryWrapper<Complaint> complaintQueryWrapper = new QueryWrapper<>();
		complaintQueryWrapper.like("user_name", userName);
		return AjaxResult.success(complaintService.list(complaintQueryWrapper));
	}

	@ApiOperation("按照投诉详情查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "cDetail", value = "业主姓名", dataType = "String")
	})
	@GetMapping("/findByDetail/{cDetail}")
	public AjaxResult findByDetail(@PathVariable String cDetail) {
		QueryWrapper<Complaint> complaintQueryWrapper = new QueryWrapper<>();
		complaintQueryWrapper.like("c_detail", cDetail);
		return AjaxResult.success(complaintService.list(complaintQueryWrapper));
	}

	@ApiOperation("按照投诉详情查询")
	@ApiImplicitParams({

	})
	@GetMapping("/findByID")
	public AjaxResult findByID() {
		QueryWrapper<Complaint> complaintQueryWrapper = new QueryWrapper<>();
		complaintQueryWrapper.like("user_ID", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId());
		return AjaxResult.success(complaintService.list(complaintQueryWrapper));
	}

}
